/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 11/19/13
 * Time: 8:18 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.interceptExercises
{
import com.societyGames.action.Action;

import flash.events.Event;

public class FishSwimAction extends Action
{
  private var _interceptRender:InterceptRender;
  private var _expression:INumberExpression;
  private var _simulationX:Number;
  private var _simulationStopX:Number;
  private const _simulationUnitsPerSecond:Number = 1;
  private var _simulationUnitsPerFrame:Number;

  public function FishSwimAction(interceptRender:InterceptRender, expression:INumberExpression, simulationStopX:Number)
  {
    this._interceptRender = interceptRender;
    this._expression = expression;
    this._simulationStopX = simulationStopX;

    var frameRate:Number = _interceptRender.stage != null ? _interceptRender.stage.frameRate : 30;
    _simulationUnitsPerFrame = _simulationUnitsPerSecond / frameRate;
  }

  public override function startAction():void
  {
    this._interceptRender.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    _simulationX = 0;
    this._interceptRender.setFishPosition(_simulationX);
  }

  private function enterFrameHandler(event:Event):void
  {
    this._simulationX += _simulationUnitsPerFrame;
    if (_simulationX > _simulationStopX) //Don't let fish go past.
    {
      _simulationX = _simulationStopX;
    }
    this._interceptRender.setFishPosition(_simulationX);
    if (_simulationX >= _simulationStopX)
    {
      this._interceptRender.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
      this._completeEventObject.dispatchEvent();
    }
  }
}
}
